level: intermediate
Is a window minimized?
In the article on Ini Files, we checked to see if a window was minimized. The API function for this is IsIconic and it is part of user32.DLL. The function requires the handle of the window, which is retrieved with the HWND() function. The IsIconic API returns true (non-zero) if the window is minimized at the time the function is called, and it returns 0 if the window is not minimized. A minimized window is sent to the user's taskbar as an icon.
handle = hwnd(#1)
calldll #user32, "IsIconic",_
handle as ulong,_ 'handle of window
result as boolean 'returns true if window is minimized
Here is a funny, little demo that uses STYLEBITS to cause a window to be minimized when it opens. It then checks to see if the window is minimized and prints the results in the mainwin.
stylebits #1, _WS_MINIMIZE,0,0,0
open "I'm Minimized" for window as #1
#1 "trapclose Quit"
handle = hwnd(#1)
calldll #user32, "IsIconic",_
handle as ulong,_ 'handle of window
result as boolean 'returns true if window is minimized
if result then
print "Window #1 is minimized."
else
print "Window #1 is NOT minimized."
end if
wait
Sub Quit handle$
close #handle$
end
end sub
Getting a window's coordinates.
In the article on Ini Files, we saved the program window's location and size for use the next time the program is run. The GetWindowRect API call from user32.DLL retrieves the coordinates of a window. It requires a handle to the window, which is obtained with Liberty BASIC's HWND() function. It also requires a struct to receive the coordinates. The members of the struct indicate the X,Y positions of the upper left corner and lower right corner of the window. The struct looks like this:
struct Rect, x1 As Long, y1 As Long, x2 As Long, y2 As Long
The function looks like this:
handle = hwnd(#1) CallDLL #user32, "GetWindowRect",handle as uLong,Rect As struct, result As Long
After the function returns, the coordinates are in the Rect struct and can be retrieved like this:
ulx = Rect.x1.struct 'left x coord uly = Rect.y1.struct 'upper y coord lrx = Rect.x2.struct 'right x coord lry = Rect.y2.struct 'lower y coord width = lrx - ulx 'right x minus left x height = lry - uly 'lower y minus upper y
Here's a little demonstration program that gets the window coordinates.
nomainwin open "test" for window as #1 #1 "trapclose Quit" struct Rect, x1 As Long, y1 As Long, x2 As Long, y2 As Long handle = hwnd(#1) CallDLL #user32, "GetWindowRect",handle as uLong,Rect As struct, result As Long ulx = Rect.x1.struct 'left x coord uly = Rect.y1.struct 'upper y coord lrx = Rect.x2.struct 'right x coord lry = Rect.y2.struct 'lower y coord width = lrx - ulx 'right x minus left x height = lry - uly 'lower y minus upper y notice "Window is at ";ulx;", ";uly;" and is ";width;" wide and ";height;" high." wait sub Quit hndle$ close #hndle$ end end sub